/* ======================================================

SWFRightClick v1.0.20120227
https://github.com/pipwerks/SWFRightClick
Copyright (c) Philip Hutchison February 2012
MIT-style license: http://pipwerks.mit-license.org/

Notes:

* Designed to replace RightClick.js by Paulius Uza
  (http://www.uza.lt) and Dan Florio (http://polygeek.com)
  http://code.google.com/p/custom-context-menu/

* Uses same technique as RightClick.js but different
  syntax and code approach

* Successfully tested in IE6-9, Safari, Firefox 3-10, and Chrome 17.

========================================================= */


var SWFRightClick = (function(){

	//Utility function
    var getTargetElement = function (e){
        e = e || window.event;
        return e.target || e.srcElement;
    };


	/*
		capture(element, includeCtrlKey)

		Disables the browser's default right-click handling when
		clicking on the specified SWF. Uses ExternalInterface to
		invoke ActionScript inside the SWF whenever the right-click
		event takes place. Also disables default ctrl-click handling
		unless specified otherwise by developer (set includeCtrlKey
		argument to false).

		Parameters:
		element [string or DOM node, required]
			* If string, pass the ID of the SWF you wish to interact with
			* If node, pass reference to SWF's DOM node (<object> or <embed> element)

		includeCtrlKey [boolean, optional]
			If set to true, SWFRightClick will handle ctrl-clicks on SWF identically to
			right-clicks. If set to false, will let browser perform default action.
			Defaults to true if not specified.

	*/

    this.capture = function (element, includeCtrlKey) {

        //If no element argument specified, end now.
        if(!element){ return; }

        //If user doesn't specify whether to check the control key, set default to true
        includeCtrlKey = (typeof includeCtrlKey === "boolean") ? includeCtrlKey : true;

        //Maintain a reference to our SWF, so we won't have to look it up over and over
        //Accepts both string and node as argument
        var theSWF = (element.nodeType && element.nodeType === 1) ? element : document.getElementById(element);

        //Ensure the target element exists before continuing.
        if(!theSWF){ return; }

        //Auto-detect the element containing the <object> generated by SWFObject.
        //All of the event handlers will be attached to this container.
        var container = theSWF.parentNode;

        if(container.addEventListener){

            /*
                W3C broswers: Webkit (Safari, Chrome), Gecko (FF), IE9+, Opera 7+

                These browsers all support element.addEventListener, as well as
                eventObject.stopPropagation and eventObject.preventDefault.
                In some cases, IE9 will not support eventObject.preventDefault,
                so a fallback `eventObject.returnValue = false` is supplied.

            */

            container.addEventListener("mousedown", function (e){

                //If this is a right-click on the SWF
                //or a control-click if includeCtrlKey == true
                if (getTargetElement(e) === theSWF && (e.button === 2 || (includeCtrlKey && e.ctrlKey))) {

                    //Prevent default behaviors: no browser context menu.
                    if(e.preventDefault){
                        e.preventDefault();
                    } else {
                        e.returnValue = false;
                    }

                    //Stop bubbling: Prevents the browser from wasting time.
                    e.stopPropagation();

                    //Since Flash won't know a right-click occured, we must inform it manually by
                    //invoking a custom callback function in your SWF via ExternalInterface
                    theSWF.rightClick();

                }

            }, true);
            //The "true" argument above enables event capturing, preventing
            //Flash Player from knowing a right-click occurred.

        } else if(container.attachEvent){

            /*
                IE8 and lower don't support addEventListener, so
                we must use attachEvent instead. Using attachEvent
                is cleaner than "document.onmousedown = function ()" and
                will not interfere with other event handlers, if any.
            */

            container.attachEvent("onmousedown", function(e) {

                //If this is a right-click on the SWF
                //or a control-click if includeCtrlKey == true
                if (getTargetElement(e) === theSWF && (e.button === 2 || (includeCtrlKey && e.ctrlKey))) {

                    //Prevent the Flash Player context menu from appearing by intercepting
                    //the click -- Flash Player never knows the click happened
                    container.setCapture();

                    //Since Flash won't know a right-click occured, we must inform it manually by
                    //invoking a custom callback function in your SWF via ExternalInterface
                    theSWF.rightClick();

                }

            });

            //Stop intercepting the clicks, enabling Flash to detect clicks again.
            container.attachEvent("onmouseup", function() {
                container.releaseCapture();
            });

            //Prevent the browser's context menu from appearing,
            //but ONLY when right-clicking or control-clicking the SWF
            container.attachEvent("oncontextmenu", function(e){
                return (getTargetElement(e) !== theSWF);
            });

        }

    };

    return this;

}());

//Wrapper for drop-in Adobe Captivate support
/*
RightClick = {
	init: function(){
		SWFRightClick.capture("Captivate");
	}
};
*/